package com.example.tellwin.base;

import android.text.TextUtils;

import com.example.tellwin.AppHelper;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Retrofit2 Cookie拦截器。用于保存和设置Cookies
 *
 * @author yuyh.
 * @date 16/8/6.
 */
public final class HeaderInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
//        Request original = chain.request();
//        if (original.url().toString().contains("book/") ||
//                original.url().toString().contains("book-list/") ||
//                original.url().toString().contains("toc/") ||
//                original.url().toString().contains("post/") ||
//                original.url().toString().contains("user/")) {
//            Request request = original.newBuilder()
//                    .addHeader("User-Agent", "ZhuiShuShenQi/3.40[preload=false;locale=zh_CN;clientidbase=android-nvidia]") // 不能转UTF-8
//                    .addHeader("X-User-Agent", "ZhuiShuShenQi/3.40[preload=false;locale=zh_CN;clientidbase=android-nvidia]")
//                    .addHeader("x-device-id", DeviceUtils.getIMEI(AppUtils.getAppContext()))
//                    .addHeader("x-device-name", "Android")
//                    .addHeader("Host", "api.zhuishushenqi.com")
//                    .addHeader("Connection", "Keep-Alive")
//                    .addHeader("If-None-Match", "W/\"2a04-4nguJ+XAaA1yAeFHyxVImg\"")
//                    .addHeader("If-Modified-Since", "Tue, 02 Aug 2016 03:20:06 UTC")
//                    .build();
//            return chain.proceed(request);
//        }
//        return chain.proceed(original);
        String token = AppHelper.getInstance().getToken();
        if (TextUtils.isEmpty(token)){
            token = "";
        }
        Request original = chain.request();
        Request.Builder requestBuilder = original.newBuilder()
                .header("platform", "Android")//平台
//                .header("x-device-id", VerifySDKDeviceUtils.getIMEI(VerifySDKAppUtils.getAppContext()))//设备信息
//                .header("x-device-name", VerifySDKDeviceUtils.getSystemModel()+"/"+ VerifySDKDeviceUtils.getSystemVersion())//设备
//                .header("x-device-platform", VerifySDKAppState.getInstance().getPlatform())//平台
                .header("token", token);//token
        Request request = requestBuilder.build();
        return chain.proceed(request);
    }
}
